package com.kczd.jinlan.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import com.kczd.jinlan.view.shopcar.ShoppingActivity;

import java.lang.reflect.Field;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * zhangy
 * Created by Administrator on 2017/5/17.
 */

public class OrderUtils {
    /**
     * 设置tablayout的长度
     * @param tabs
     * @param leftDip
     * @param rightDip
     */
    public static void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
   public static void showOrderGo(final Context context){

       new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE).setTitleText("提示")
               .setContentText("已添加到购物车？")
               .setCancelText("留在本页")
               .setConfirmText("我要去购物车")
               .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                   @Override
                   public void onClick(SweetAlertDialog sDialog) {
                     context.startActivity( new Intent(context, ShoppingActivity.class));
                       sDialog.cancel();
                   }
               }).showCancelButton(true)
               .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                   @Override
                   public void onClick(SweetAlertDialog sDialog) {
                       sDialog.cancel();
                   }
               }).show();
   }
}
